/**
 * 
 */

package com.wutianyi.thrift.example;

import java.util.HashMap;

import org.apache.thrift.TException;

import com.wutianyi.thrift.shared.SharedStruct;
import com.wutianyi.thrift.tutorial.Calculator;
import com.wutianyi.thrift.tutorial.InvalidOperation;
import com.wutianyi.thrift.tutorial.Work;

/**
 * @author wutianyi
 * 
 */
public class CalculatorHandler implements Calculator.Iface {

	private HashMap<Integer, SharedStruct>	log;

	public CalculatorHandler() {

		log = new HashMap<Integer, SharedStruct>();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wutianyi.thrift.tutorial.Calculator.Iface#add(int, int)
	 */
	@Override
	public int add(int num1, int num2) throws TException {

		System.out.println("add(" + num1 + "," + num2 + ")");
		return num1 + num2;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wutianyi.thrift.tutorial.Calculator.Iface#calculate(int,
	 * com.wutianyi.thrift.tutorial.Work)
	 */
	@Override
	public int calculate(int logid, Work work) throws InvalidOperation, TException {

		System.out.println("calculate(" + logid + ", {" + work.op + "," + work.num1 + ","
				+ work.num2 + "})");
		int val = 0;
		switch(work.op) {
			case ADD:
				val = work.num1 + work.num2;
				break;
			case SUBTRACT:
				val = work.num1 - work.num2;
				break;
			case MULTIPLY:
				val = work.num1 * work.num2;
				break;
			case DIVIDE:
				if(work.num2 == 0) {
					InvalidOperation io = new InvalidOperation();
					io.what = work.op.getValue();
					io.why = "Cannot divide by 0";
					throw io;
				}
				val = work.num1 / work.num2;
				break;
			default:
				InvalidOperation io = new InvalidOperation();
				io.what = work.op.getValue();
				io.why = "Unknown operation";
				throw io;
		}

		SharedStruct entry = new SharedStruct();
		entry.key = logid;
		entry.value = Integer.toString(val);
		log.put(logid, entry);

		return val;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wutianyi.thrift.tutorial.Calculator.Iface#ping()
	 */
	@Override
	public void ping() throws TException {

		System.out.println("ping()");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wutianyi.thrift.tutorial.Calculator.Iface#zip()
	 */
	@Override
	public void zip() throws TException {

		System.out.println("zip()");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.wutianyi.thrift.shared.SharedService.Iface#getStruct(int)
	 */
	@Override
	public SharedStruct getStruct(int key) throws TException {
		System.out.println("getStruct(" + key + ")");
		return log.get(key);
	}

}
